/*
 * Copyright 2005-2013 finalist.cn. All rights reserved.
 * Support: http://www.finalist.cn
 * License: http://www.finalist.cn/license
 */
package com.webapp.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.webapp.entity.Area;


/**
 * Service - 地区
 * 
 * @author Finalist IT Group
 * @version 3.0
 */
@Service("areaService")
public class AreaService extends AbstractService {
	private List<Area> areas=new ArrayList<Area>();
	
	@Transactional(readOnly = true)
	public List<Area> findRoots() {
		return areaDao.findRoots(null);
	}
	
	@Transactional(readOnly = true)
	public Area find(Long id) {
		return areaDao.get(id);
	}

	
	@Transactional(readOnly = true)
	public List<Area> findRoots(Integer count) {
		return areaDao.findRoots(count);
	}

	@Transactional
	@CacheEvict(value = "area", allEntries = true)
	public void save(Area area) {
		//super.save(area);
		areaDao.save(area);
	}

	/*@Transactional
	@CacheEvict(value = "area", allEntries = true)
	public Area update(Area area) {
		return areaDao.update(area);
	}

	@Transactional
	@CacheEvict(value = "area", allEntries = true)
	public Area update(Area area, String... ignoreProperties) {
		return areaDao.update(area, ignoreProperties);
	}*/

	@Transactional
	@CacheEvict(value = "area", allEntries = true)
	public void delete(Long id) {
		areaDao.delete(id);
	}

	@Transactional
	@CacheEvict(value = "area", allEntries = true)
	public void delete(Long... ids) {
		areaDao.delete(ids);
	}

	@Transactional
	@CacheEvict(value = "area", allEntries = true)
	public void delete(Area area) {
		areaDao.delete(area);
	}

	public List<Area> findChildrens(Long parentId) {
		return areaDao.findChildrens(parentId);
	}
	
	public List<Area> loadAll() {
		return areaDao.loadAll();
	}
	
	@PostConstruct
	protected void inintArea(){
		List<Area> allArea=areaDao.loadAll();
		if(allArea!=null&&allArea.size()!=0){
			for(Area area:allArea){
				if(area.getTreePath().split(",")!=null&&area.getTreePath().split(",").length>2){
					areas.add(area);
				}
			}
		}
	}
	
	
	public List<Area> findAll(){
		return areas;
	}
	
	public List<Area> findAllAreas(){
		List<Area> allAreas=new ArrayList<Area>();
		List<Area> allArea=areaDao.loadAll();
		if(allArea!=null&&allArea.size()!=0){
			for(Area area:allArea){
				if(area.getTreePath().split(",")!=null&&area.getTreePath().split(",").length>2){
					allAreas.add(area);
				}
			}
		}
		return allAreas;
	}
	
	public List<Area> findAllBranch(){
		
		return areaDao.findChildrens(1l);	//CHO的id为1
	}
	public Area getByCode(String code){
		return areaDao.getAreaByCode(code);
	}
	public Area getByName(String name){
		return areaDao.getAreaByName(name);
	}
}